(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('lfe.ppfRepayment.createBankVerifyController', createBankVerifyController);
	createBankVerifyController.$inject = [ '$q', '$filter', '$scope', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'ppfRepaymentService', 'dateService', "arithService", 'commonService'];
	function createBankVerifyController($q, $filter, $scope, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService, ppfRepaymentService, dateService, arithService, commonService) {
		var ctrl = this, params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.ppfRepaymentLoad = {};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.loansCfg.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.ppfRepaymentLoad.ppfRepayments';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			ppfRepaymentService.getCurrencyOptions().then(function(response) {
				ctrl.currencyOptions = response.data;
				authService.createBankVerify(params).then(function(response) {
					if (response.data && response.data.model && response.data.task) {
						ctrl.ppfRepaymentLoad= response.data.model;
						ctrl.ppfRepaymentLoad.taskId = response.data.task.taskId;
					}
				});
			});
		};
		
		var watcher = $scope.$watch('vm.ppfRepaymentLoad.ppfRepayments.length', function(modelValue) {
			if(modelValue || modelValue == 0){
				ctrl.ppfRepaymentLoad.totalLoanAmt = calTotalLoanAmt(ctrl.ppfRepaymentLoad.ppfRepayments);
			}
		});
		$scope.$on('$destroy', function() {
			(watcher || angular.noop)();
		});
		
		function calTotalLoanAmt(ppfRepayments){
			var totalLoanAmt = 0;
			angular.forEach(ppfRepayments, function(value, key) {
				totalLoanAmt = parseFloat(arithService.add(totalLoanAmt, value.loanOutAmount));
			});
			return totalLoanAmt;
		};

		ctrl.createBankVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.ppfRepaymentLoad.message = comments;
						authService.createBankVerifyReject(ctrl.ppfRepaymentLoad).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createBankVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.ppfRepaymentLoad.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createBankVerifyApprove(ctrl.ppfRepaymentLoad).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('lfe.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'lfe.workflow.wfTxn.list',
					'params' : {}
				}]
			});
		}
		
		
		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('lfe.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'lfe.workflow.wfTxn.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
